import { ColorRule } from './types';

export enum ColorEnum {
  BLACK = '\x1b[30m', // 黑色
  RED = '\x1b[31m', // 红色
  GREEN = '\x1b[32m', // 绿色
  YELLOW = '\x1b[33m', // 黄色
  BLUE = '\x1b[34m', // 蓝色
  MAGENTA = '\x1b[35m', // 紫色
  CYAN = '\x1b[36m', // 深绿
  WHITE = '\x1b[37m', // 白色
  RESET = '\x1b[0m', // 恢复到默认属性
}

export function getTextWithColor(text: string | null | undefined, rules?: ColorRule[]): string {
  if (!Array.isArray(rules) || rules.length === 0) return text || '';
  if (!text) return '';
  let lable = text;
  rules.forEach((rule) => {
    const { color, pattern } = rule;
    lable = lable.replace(pattern, (word) => {
      return `${ColorEnum[color]}${word}${ColorEnum.RESET}`;
    });
  });
  // const colorRule = rules.find((rule) => rule.validator(text));
  // if (colorRule) {
  //   return `${ColorEnum[colorRule.color]}${text}${ColorEnum.RESET}`;
  // }
  return lable;
}
